{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 一、解析语法的基本结构\n",
    "## 1.列表解析\n",
    "[experssion for item in iterable]\n",
    "三要素：表达式、可迭代对象、if条件\n",
    "执行过程：\n",
    "1.遍历可迭代对象\n",
    "2.通过if条件对表达式进行筛选，若通过筛选，则把元素传递给表达式，若未通过，则进入步骤1，进行下一次迭代\n",
    "3.将传递给表达式的元素代入表达式进行处理，产生一个结果\n",
    "4.将3的结果添加到列表中\n",
    "5.直至迭代对象迭代结束，返回列表\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
     ]
    }
   ],
   "source": [
    "square = [x**2 for x in range(1,11)]\n",
    "print(square)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T08:51:05.783152200Z",
     "start_time": "2024-11-23T08:51:05.767148500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.条件表达式\n",
    "exprl if condition else expr2"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "10"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = -10\n",
    "x=n if n>0 else -n\n",
    "x"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T08:57:26.576007800Z",
     "start_time": "2024-11-23T08:57:26.549968900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 二、三大神器\n",
    "## 3.1生成器\n",
    " 生成器是一种特殊的迭代器，它可以在不占用大量内存的情况下，进行数据的生成，生成器是一种延迟计算的机制，只有在需要的时候才计算，从而节省大量的内存。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 1.生成器表达式\n",
    "#求 0-100的和\n",
    "sum(x for x in range(101))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 2.生成器函数\n",
    "#生成斐波那契数列\n",
    "def fib(max):\n",
    "    n,a,b=0,0,1\n",
    "    while n<max:\n",
    "        yield b#执行到第10次时，返回b，之后便停在这里不再执行\n",
    "        a,b=b,a+b\n",
    "        n+=1\n",
    "fib(10)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2迭代器\n",
    "### 1.可迭代对象\n",
    "#可迭代对象是可以被for循环遍历的对象。\n",
    "可以使用isinstance()函数来判断一个对象是否是可迭代对象。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections.abc import Iterable\n",
    "isinstance([],Iterable)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T09:04:21.540566100Z",
     "start_time": "2024-11-23T09:04:21.521051600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.迭代器\n",
    "迭代器是可以被next()函数调用并不断返回下一个值,直到没有数据可以取的对象称为迭代器\n",
    "（1）生成器都是迭代器\n",
    "（2）通过iter()函数可以生成迭代器对象：iter(iterable)\n",
    "for item in iter(iterable):等价于\n",
    "先通过iter()函数获取迭代器对象，再通过next()函数获取迭代器对象的下一个迭代element，并将其赋值给item\n",
    "（3）zip enumerate等itertools模块中的函数都是迭代器\n",
    "(4)文件是迭代器\n",
    "（5）迭代器可以被耗尽的\n",
    "（6）range不是迭代器，range是懒序列"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(iter([1,2,3]),Iterable)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T09:09:07.923473400Z",
     "start_time": "2024-11-23T09:09:07.875383800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.3装饰器\n",
    "### 1.函数对象\n",
    "函数是Python中的第一类对象,运行下面代码可以发现fun是fuction的一个实例\n",
    "(1)可以把函数赋值给一个变量\n",
    "(2)对该变量进行调用，可以实现原函数的功能"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'function'>\n"
     ]
    }
   ],
   "source": [
    "def func():\n",
    "    print('hello world')\n",
    "print(type(func))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T09:18:45.911859900Z",
     "start_time": "2024-11-23T09:18:45.865727100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "None\n",
      "hello world\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "pow = func\n",
    "print(pow())\n",
    "print(func())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T09:22:19.780210900Z",
     "start_time": "2024-11-23T09:22:19.746110700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.高阶函数\n",
    "（1）接受函数作为参数\n",
    "（2）返回一个函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "def add(x,y,f):\n",
    "    return f(x)+f(y)\n",
    "print(add(1,2,abs))#abs是一个函数\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T09:24:14.768776500Z",
     "start_time": "2024-11-23T09:24:14.728878800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.嵌套函数\n",
    "在函数内部定义的函数称为嵌套函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "outer\n"
     ]
    }
   ],
   "source": [
    "def outer():\n",
    "    print('outer')\n",
    "    def inner():\n",
    "       print('inner')\n",
    "\n",
    "outer()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T09:26:17.691296900Z",
     "start_time": "2024-11-23T09:26:17.640948300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.简单装饰器"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "0.0010006427764892578\n"
     ]
    }
   ],
   "source": [
    "#嵌套函数实现\n",
    "import time\n",
    "def timer(func):\n",
    "    def wrapper():\n",
    "        start = time.time()\n",
    "        func()\n",
    "        end = time.time()\n",
    "        print(end-start)\n",
    "    return wrapper\n",
    "def f1 ():\n",
    "    print('hello')\n",
    "\n",
    "f1 = timer(f1)#此时的f1函数包括了timer和wrapper中的所有功能，变量\n",
    "f1()#在不改变f1的情况下，实现了测试其执行时间的功能"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T09:53:56.509161900Z",
     "start_time": "2024-11-23T09:53:56.490136500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "0.0006573200225830078\n"
     ]
    }
   ],
   "source": [
    "## 语法糖\n",
    "import time\n",
    "def timer(func):\n",
    "    def wrapper():\n",
    "        start = time.time()\n",
    "        func()\n",
    "        end = time.time()\n",
    "        print(end-start)\n",
    "    return wrapper\n",
    "\n",
    "@timer#语法糖\n",
    "def f1 ():\n",
    "    print('hello')\n",
    "\n",
    "\n",
    "f1()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-23T09:57:45.080340400Z",
     "start_time": "2024-11-23T09:57:45.062327900Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
